import {
	createRouter,
	createWebHistory
} from 'vue-router'
import HomeView from '../views/HomeView.vue'
const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: '/',
			name: 'home',
			component: HomeView,
			redirect:"/marketSys"
			//redirect:'/setting'
		},	
		{
			path: "/login",
			name: 'login',
			component: () => import("../views/Login.vue")
		}
		
	]
})
import { getAllMenuList } from '@/api'

// console.log(menuRes)

//全局前置守卫；

router.beforeEach(async (to,from)=>{
	if(to.name!='login' && !sessionStorage.getItem("token")){
		return {name:"login"}
	}
	if(!sessionStorage.getItem("token")){
		return
	}
	
	if(router.getRoutes().length==2){
		let menuRes=await getAllMenuList({
			roleId:Number(sessionStorage.getItem("roleId"))
		})
		// console.log('mes',menuRes)
		menuRes.forEach(first=>{
			//console.log(first)
			let firstMenuName=first.menuPath
			firstMenuName=firstMenuName.substring(1);//一级菜单名称
			
			router.addRoute('home',{
				name:firstMenuName,
				path:first.menuPath,
				redirect:first.childs[0] ? first.childs[0].menuPath : "",
				component:()=>import(`../views/${firstMenuName}.vue`)
			})
			if(first.childs && first.childs.length!=0){
				//console.log(first.childs)
				first.childs.forEach(second=>{
					let secondMenuName=second.menuPath.split("/")
					secondMenuName=secondMenuName[2]
					// console.log('second',secondMenuName)
					var routeObj={
						name:secondMenuName,
						path:second.menuPath,
						component:()=>import(`../views/${secondMenuName}.vue`),
						// meta:{
						// 		btns:second.childs.length==0 ? [] : second.childs
						// 	}
						//redirect:second.childs[0] ? second.childs[0].menuPath : "",
						//anniu
					}
					var btns=[]
					second.childs.forEach(sv=>{
						if(sv.menuType=="按钮"){
							btns.push(sv)
						}
					})
					//console.log("second",second.childs)
					routeObj.meta={
						btns,
					}
					if(second.childs.length!=0){
						if(second.childs[0].menuType!="按钮"){
							routeObj.redirect=second.childs[0].menuPath
						}
					}
					//console.log('obj',routeObj)
					router.addRoute(firstMenuName,routeObj)
					// console.log(second)
					if(second.childs && second.childs.length!=0){
						second.childs.forEach(third=>{
							let thirdName=third.menuPath.split("/")
							thirdName=thirdName[3]
							// console.log('thirdName',third.menuPath,thirdName)
							let curRoute=router.getRoutes().filter(v=>v.path==third.menuPath)
							// console.log(router.getRoutes(),curRoute)
							if(curRoute.length!=0){
								return
							}
							
							router.addRoute(firstMenuName,{
								name:thirdName,
								path:third.menuPath,
								component:()=>import(`../views/${thirdName}.vue`),
								meta:{
										btns:third.childs.length==0 ? [] : third.childs
									}
							})
						})
					}
				})
				
			}
		})
		  // console.log('getRoutes',router.getRoutes())
		router.replace(to.path)
	}
	// console.log(to)
	// console.log("全局前置守卫,beforeEach....",to,from)
	
})

export default router

// import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
// import { getAllMenuList } from '@/api'

// const router = createRouter({
// 	history: createWebHistory(import.meta.env.BASE_URL),
// 	routes: [
// 		{
// 			path: '/',
// 			name: 'home',
// 			component: HomeView,
// 			redirect: "/marketSys"
// 		},
// 		{
// 			path: "/login",
// 			name: 'login',
// 			component: () => import("../views/Login.vue")
// 		}
// 	]
// })

// // 全局导航守卫
// router.beforeEach(async (to, from) => {
// 	const token = sessionStorage.getItem("token");
// 	if (to.name !== 'login' && !token) {
// 		return { name: "login" };
// 	}
// 	if (!token) {
// 		return;
// 	}

// 	if (router.getRoutes().length === 2) {
// 		let menuRes = await getAllMenuList({ roleId: Number(sessionStorage.getItem("roleId")) });
// 		console.log('菜单响应：', menuRes);

// 		menuRes.forEach(first => {
// 			let firstMenuName = first.menuPath.substring(1); // 提取一级菜单名称

// 			router.addRoute('home', {
// 				name: firstMenuName,
// 				path: first.menuPath,
// 				component: () => import(`../views/${firstMenuName}.vue`),
// 				redirect: first.childs && first.childs.length ? first.childs[0].menuPath : "",
// 			});

// 			first.childs && first.childs.forEach(second => {
// 				let secondMenuName = second.menuPath.split("/")[2];
// 				let routeObj = {
// 					name: secondMenuName,
// 					path: second.menuPath,
// 					component: () => import(`../views/${secondMenuName}.vue`),
// 					meta: {
// 						btns: second.childs.filter(child => child.menuType === "按钮")
// 					}
// 				};

// 				if (second.childs.length && second.childs[0].menuType !== "按钮") {
// 					routeObj.redirect = second.childs[0].menuPath;
// 				}

// 				if (!routeObj.redirect) {
// 					delete routeObj.redirect; // 删除不需要的重定向
// 				}

// 				router.addRoute(firstMenuName, routeObj);

// 				second.childs && second.childs.forEach(third => {
// 					let thirdName = third.menuPath.split("/")[3];

// 					if (!router.getRoutes().some(route => route.path === third.menuPath)) {
// 						router.addRoute(firstMenuName, {
// 							name: thirdName,
// 							path: third.menuPath,
// 							component: () => import(`../views/${thirdName}.vue`),
// 							meta: {
// 								btns: third.childs.length ? third.childs : []
// 							}
// 						});
// 					}
// 				});
// 			});
// 		});
// 		console.log('当前路由：', router.getRoutes());
// 		router.replace(to.path);
// 	}
// });

// export default router;

